package cn.tedu.weibo.controller;


import cn.tedu.weibo.mapper.WeiboMapper;
import cn.tedu.weibo.pojo.dto.WeiboDTO;
import cn.tedu.weibo.pojo.entity.Weibo;
import cn.tedu.weibo.pojo.vo.UserVO;
import cn.tedu.weibo.pojo.vo.WeiboDetailVO;
import cn.tedu.weibo.pojo.vo.WeiboListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
public class WeiboController {
    @Autowired(required = false)
    private WeiboMapper mapper;


    @RequestMapping("/weibo/insert")
    public int insert(@RequestBody WeiboDTO weiboDTO, HttpSession session){

        UserVO user = (UserVO) session.getAttribute("user");
        if (user==null){
            return 2;
        }
        System.out.println("weiboDTO:"+weiboDTO);

        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);

        weibo.setCreated(new Date());
        System.out.println("weibo:" + weibo);
        weibo.setUserId(user.getId());
        mapper.insert(weibo);
        return 1;

    }
    @RequestMapping("/selectAllWeibo")
    public List<WeiboListVO> selectAllWeibo(){
        return mapper.queryAll();
    }

    @RequestMapping("/weibo/selectById")
    private WeiboDetailVO selectById(int id){
        return mapper.queryById(id);
    }


}
